{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e201ac33",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import math\n",
    "import time\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e26ea598",
   "metadata": {},
   "source": [
    "## Exercise1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "68f71469",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv(\"gaze.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4330ee6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_copy = data.copy(deep=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3ad3d7ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "# confidence为信度 去除信度低于0.9的（不包括0.9）的数据 输出剩余数据条数\n",
    "for i in range(125264):\n",
    "    if data_copy[\"confidence\"].loc[i] < 0.9:\n",
    "        data_copy.drop(i, axis=0, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f3e21787",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "87442"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(data_copy)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c71b197c",
   "metadata": {},
   "source": [
    "## Exercise2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a6844214",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 重新排序并且把原来的index丢弃 否则索引过程中仍然会使用原索引 \n",
    "data_copy.reset_index(drop = True, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "07bcbb4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 求出此DataFrame的基本信息\n",
    "x = data_copy.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e03e163c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 索引此DataFrame的平均值和方差\n",
    "npx_mean = x[\"norm_pos_x\"].loc[\"mean\"]\n",
    "npx_sigma = x[\"norm_pos_x\"].loc[\"std\"]\n",
    "npy_mean = x[\"norm_pos_y\"].loc[\"mean\"]\n",
    "npy_sigma = x[\"norm_pos_y\"].loc[\"std\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f709e4f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 利用3 sigma原则去除无效值\n",
    "sxl = npx_mean - 3*npx_sigma\n",
    "sxr = npx_mean + 3*npx_sigma\n",
    "syl = npy_mean - 3*npy_sigma\n",
    "syr = npy_mean + 3*npy_sigma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1f1a4efe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 去除无效值\n",
    "for i in range(87442):\n",
    "    tmpx = data_copy[\"norm_pos_x\"].loc[i]\n",
    "    tmpy = data_copy[\"norm_pos_y\"].loc[i]\n",
    "    if tmpx < sxl or tmpx > sxr or tmpy < syl or tmpy > syr:\n",
    "        data_copy.drop(i, axis=0, inplace=True)\n",
    "\n",
    "# 重新排序并且把原来的index丢弃 否则索引过程中仍然会使用原索引 \n",
    "data_copy.reset_index(drop = True, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "88e6977f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "86185\n"
     ]
    }
   ],
   "source": [
    "# 问题2：输出去除无效值后的数据条数\n",
    "print(len(data_copy))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f48c9442",
   "metadata": {},
   "source": [
    "## Exercise3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c8a6cad2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 时间戳(s)转换为特定格式的时间\n",
    "# datetime.datetime.fromtimestamp(88150.825638, datetime.timezone.utc).strftime(\"%Y-%m-%dT %H:%M:%S.%f%z\")\n",
    "# 需要指定timezone.utc为标准\n",
    "# time.struct_time(tm_year=1970, tm_mon=1, tm_mday=2, tm_hour=8, tm_min=29, tm_sec=10, tm_wday=4, tm_yday=2, tm_isdst=0)\n",
    "# 注意localtime只能精确到seconds而不能精确到毫秒 所以往后面添加 .%f 会出错\n",
    "data_copy_1 = data_copy.copy(deep=True)\n",
    "data_copy_1[\"gaze_timestamp\"] = data_copy_1[\"gaze_timestamp\"].apply(lambda x: datetime.datetime.fromtimestamp(x, datetime.timezone.utc).strftime(\"%Y-%m-%dT %H:%M:%S.%f%z\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "eb1769ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>gaze_timestamp</th>\n",
       "      <th>world_index</th>\n",
       "      <th>confidence</th>\n",
       "      <th>norm_pos_x</th>\n",
       "      <th>norm_pos_y</th>\n",
       "      <th>base_data</th>\n",
       "      <th>gaze_point_3d_x</th>\n",
       "      <th>gaze_point_3d_y</th>\n",
       "      <th>gaze_point_3d_z</th>\n",
       "      <th>eye_center0_3d_x</th>\n",
       "      <th>...</th>\n",
       "      <th>eye_center0_3d_z</th>\n",
       "      <th>gaze_normal0_x</th>\n",
       "      <th>gaze_normal0_y</th>\n",
       "      <th>gaze_normal0_z</th>\n",
       "      <th>eye_center1_3d_x</th>\n",
       "      <th>eye_center1_3d_y</th>\n",
       "      <th>eye_center1_3d_z</th>\n",
       "      <th>gaze_normal1_x</th>\n",
       "      <th>gaze_normal1_y</th>\n",
       "      <th>gaze_normal1_z</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1970-01-02T 00:29:10.825638+0000</td>\n",
       "      <td>2</td>\n",
       "      <td>0.915197</td>\n",
       "      <td>0.493612</td>\n",
       "      <td>0.398950</td>\n",
       "      <td>88150.827729-0 88150.823548-1</td>\n",
       "      <td>2.412810</td>\n",
       "      <td>-21.468145</td>\n",
       "      <td>-295.071057</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>-20.000000</td>\n",
       "      <td>0.062883</td>\n",
       "      <td>0.025100</td>\n",
       "      <td>0.997705</td>\n",
       "      <td>-39.934928</td>\n",
       "      <td>14.997919</td>\n",
       "      <td>-20.075283</td>\n",
       "      <td>-0.150041</td>\n",
       "      <td>0.234536</td>\n",
       "      <td>0.960458</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1970-01-02T 00:29:10.827729+0000</td>\n",
       "      <td>2</td>\n",
       "      <td>0.915197</td>\n",
       "      <td>0.493561</td>\n",
       "      <td>0.399305</td>\n",
       "      <td>88150.827729-0 88150.827729-1</td>\n",
       "      <td>2.429837</td>\n",
       "      <td>-21.374413</td>\n",
       "      <td>-294.819200</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>-20.000000</td>\n",
       "      <td>0.062883</td>\n",
       "      <td>0.025100</td>\n",
       "      <td>0.997705</td>\n",
       "      <td>-39.934928</td>\n",
       "      <td>14.997919</td>\n",
       "      <td>-20.075283</td>\n",
       "      <td>-0.150243</td>\n",
       "      <td>0.234115</td>\n",
       "      <td>0.960530</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1970-01-02T 00:29:10.829663+0000</td>\n",
       "      <td>2</td>\n",
       "      <td>0.915197</td>\n",
       "      <td>0.493570</td>\n",
       "      <td>0.399542</td>\n",
       "      <td>88150.827729-0 88150.83159799999-1</td>\n",
       "      <td>2.427118</td>\n",
       "      <td>-21.328193</td>\n",
       "      <td>-294.874516</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>-20.000000</td>\n",
       "      <td>0.062883</td>\n",
       "      <td>0.025100</td>\n",
       "      <td>0.997705</td>\n",
       "      <td>-39.934928</td>\n",
       "      <td>14.997919</td>\n",
       "      <td>-20.075283</td>\n",
       "      <td>-0.150210</td>\n",
       "      <td>0.233747</td>\n",
       "      <td>0.960624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1970-01-02T 00:29:10.866015+0000</td>\n",
       "      <td>3</td>\n",
       "      <td>0.922197</td>\n",
       "      <td>0.492592</td>\n",
       "      <td>0.398705</td>\n",
       "      <td>88150.867514-0 88150.86451599999-1</td>\n",
       "      <td>2.827911</td>\n",
       "      <td>-21.750938</td>\n",
       "      <td>-298.233245</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>-20.000000</td>\n",
       "      <td>0.060717</td>\n",
       "      <td>0.025220</td>\n",
       "      <td>0.997836</td>\n",
       "      <td>-39.934928</td>\n",
       "      <td>14.997919</td>\n",
       "      <td>-20.075283</td>\n",
       "      <td>-0.149814</td>\n",
       "      <td>0.233510</td>\n",
       "      <td>0.960744</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1970-01-02T 00:29:10.869620+0000</td>\n",
       "      <td>3</td>\n",
       "      <td>0.922197</td>\n",
       "      <td>0.492635</td>\n",
       "      <td>0.398814</td>\n",
       "      <td>88150.867514-0 88150.871726-1</td>\n",
       "      <td>2.813774</td>\n",
       "      <td>-21.744854</td>\n",
       "      <td>-298.471253</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>-20.000000</td>\n",
       "      <td>0.060717</td>\n",
       "      <td>0.025220</td>\n",
       "      <td>0.997836</td>\n",
       "      <td>-39.934928</td>\n",
       "      <td>14.997919</td>\n",
       "      <td>-20.075283</td>\n",
       "      <td>-0.149645</td>\n",
       "      <td>0.233258</td>\n",
       "      <td>0.960831</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86180</th>\n",
       "      <td>1970-01-02T 00:34:03.695600+0000</td>\n",
       "      <td>8788</td>\n",
       "      <td>0.995336</td>\n",
       "      <td>0.407831</td>\n",
       "      <td>0.413470</td>\n",
       "      <td>88443.69561-0 88443.695591-1</td>\n",
       "      <td>327.648915</td>\n",
       "      <td>-173.027524</td>\n",
       "      <td>-2777.243340</td>\n",
       "      <td>19.888755</td>\n",
       "      <td>...</td>\n",
       "      <td>-20.707271</td>\n",
       "      <td>-0.110854</td>\n",
       "      <td>0.041843</td>\n",
       "      <td>0.992955</td>\n",
       "      <td>-37.372594</td>\n",
       "      <td>14.763454</td>\n",
       "      <td>-23.294339</td>\n",
       "      <td>-0.130868</td>\n",
       "      <td>0.093021</td>\n",
       "      <td>0.987026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86181</th>\n",
       "      <td>1970-01-02T 00:34:03.697570+0000</td>\n",
       "      <td>8788</td>\n",
       "      <td>0.948733</td>\n",
       "      <td>0.407552</td>\n",
       "      <td>0.414731</td>\n",
       "      <td>88443.69561-0 88443.69953099999-1</td>\n",
       "      <td>311.054477</td>\n",
       "      <td>-161.381388</td>\n",
       "      <td>-2628.637408</td>\n",
       "      <td>19.888755</td>\n",
       "      <td>...</td>\n",
       "      <td>-20.707271</td>\n",
       "      <td>-0.110854</td>\n",
       "      <td>0.041843</td>\n",
       "      <td>0.992955</td>\n",
       "      <td>-37.372594</td>\n",
       "      <td>14.763454</td>\n",
       "      <td>-23.294339</td>\n",
       "      <td>-0.132037</td>\n",
       "      <td>0.091860</td>\n",
       "      <td>0.986979</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86182</th>\n",
       "      <td>1970-01-02T 00:34:03.701580+0000</td>\n",
       "      <td>8788</td>\n",
       "      <td>0.945284</td>\n",
       "      <td>0.407467</td>\n",
       "      <td>0.414705</td>\n",
       "      <td>88443.703629-0 88443.69953099999-1</td>\n",
       "      <td>313.566509</td>\n",
       "      <td>-162.585330</td>\n",
       "      <td>-2647.420719</td>\n",
       "      <td>19.888755</td>\n",
       "      <td>...</td>\n",
       "      <td>-20.707271</td>\n",
       "      <td>-0.111009</td>\n",
       "      <td>0.041794</td>\n",
       "      <td>0.992940</td>\n",
       "      <td>-37.372594</td>\n",
       "      <td>14.763454</td>\n",
       "      <td>-23.294339</td>\n",
       "      <td>-0.132037</td>\n",
       "      <td>0.091860</td>\n",
       "      <td>0.986979</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86183</th>\n",
       "      <td>1970-01-02T 00:34:03.703608+0000</td>\n",
       "      <td>8788</td>\n",
       "      <td>0.945284</td>\n",
       "      <td>0.407160</td>\n",
       "      <td>0.415340</td>\n",
       "      <td>88443.703629-0 88443.703587-1</td>\n",
       "      <td>297.019831</td>\n",
       "      <td>-152.352987</td>\n",
       "      <td>-2499.428189</td>\n",
       "      <td>19.888755</td>\n",
       "      <td>...</td>\n",
       "      <td>-20.707271</td>\n",
       "      <td>-0.111009</td>\n",
       "      <td>0.041794</td>\n",
       "      <td>0.992940</td>\n",
       "      <td>-37.372594</td>\n",
       "      <td>14.763454</td>\n",
       "      <td>-23.294339</td>\n",
       "      <td>-0.133311</td>\n",
       "      <td>0.091656</td>\n",
       "      <td>0.986827</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86184</th>\n",
       "      <td>1970-01-02T 00:34:03.705574+0000</td>\n",
       "      <td>8788</td>\n",
       "      <td>0.945284</td>\n",
       "      <td>0.407637</td>\n",
       "      <td>0.413674</td>\n",
       "      <td>88443.703629-0 88443.70751899999-1</td>\n",
       "      <td>323.629974</td>\n",
       "      <td>-170.142461</td>\n",
       "      <td>-2737.404466</td>\n",
       "      <td>19.888755</td>\n",
       "      <td>...</td>\n",
       "      <td>-20.707271</td>\n",
       "      <td>-0.111009</td>\n",
       "      <td>0.041794</td>\n",
       "      <td>0.992940</td>\n",
       "      <td>-37.372594</td>\n",
       "      <td>14.763454</td>\n",
       "      <td>-23.294339</td>\n",
       "      <td>-0.131320</td>\n",
       "      <td>0.092941</td>\n",
       "      <td>0.986974</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>86185 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                         gaze_timestamp  world_index  confidence  norm_pos_x  \\\n",
       "0      1970-01-02T 00:29:10.825638+0000            2    0.915197    0.493612   \n",
       "1      1970-01-02T 00:29:10.827729+0000            2    0.915197    0.493561   \n",
       "2      1970-01-02T 00:29:10.829663+0000            2    0.915197    0.493570   \n",
       "3      1970-01-02T 00:29:10.866015+0000            3    0.922197    0.492592   \n",
       "4      1970-01-02T 00:29:10.869620+0000            3    0.922197    0.492635   \n",
       "...                                 ...          ...         ...         ...   \n",
       "86180  1970-01-02T 00:34:03.695600+0000         8788    0.995336    0.407831   \n",
       "86181  1970-01-02T 00:34:03.697570+0000         8788    0.948733    0.407552   \n",
       "86182  1970-01-02T 00:34:03.701580+0000         8788    0.945284    0.407467   \n",
       "86183  1970-01-02T 00:34:03.703608+0000         8788    0.945284    0.407160   \n",
       "86184  1970-01-02T 00:34:03.705574+0000         8788    0.945284    0.407637   \n",
       "\n",
       "       norm_pos_y                           base_data  gaze_point_3d_x  \\\n",
       "0        0.398950       88150.827729-0 88150.823548-1         2.412810   \n",
       "1        0.399305       88150.827729-0 88150.827729-1         2.429837   \n",
       "2        0.399542  88150.827729-0 88150.83159799999-1         2.427118   \n",
       "3        0.398705  88150.867514-0 88150.86451599999-1         2.827911   \n",
       "4        0.398814       88150.867514-0 88150.871726-1         2.813774   \n",
       "...           ...                                 ...              ...   \n",
       "86180    0.413470        88443.69561-0 88443.695591-1       327.648915   \n",
       "86181    0.414731   88443.69561-0 88443.69953099999-1       311.054477   \n",
       "86182    0.414705  88443.703629-0 88443.69953099999-1       313.566509   \n",
       "86183    0.415340       88443.703629-0 88443.703587-1       297.019831   \n",
       "86184    0.413674  88443.703629-0 88443.70751899999-1       323.629974   \n",
       "\n",
       "       gaze_point_3d_y  gaze_point_3d_z  eye_center0_3d_x  ...  \\\n",
       "0           -21.468145      -295.071057         20.000000  ...   \n",
       "1           -21.374413      -294.819200         20.000000  ...   \n",
       "2           -21.328193      -294.874516         20.000000  ...   \n",
       "3           -21.750938      -298.233245         20.000000  ...   \n",
       "4           -21.744854      -298.471253         20.000000  ...   \n",
       "...                ...              ...               ...  ...   \n",
       "86180      -173.027524     -2777.243340         19.888755  ...   \n",
       "86181      -161.381388     -2628.637408         19.888755  ...   \n",
       "86182      -162.585330     -2647.420719         19.888755  ...   \n",
       "86183      -152.352987     -2499.428189         19.888755  ...   \n",
       "86184      -170.142461     -2737.404466         19.888755  ...   \n",
       "\n",
       "       eye_center0_3d_z  gaze_normal0_x  gaze_normal0_y  gaze_normal0_z  \\\n",
       "0            -20.000000        0.062883        0.025100        0.997705   \n",
       "1            -20.000000        0.062883        0.025100        0.997705   \n",
       "2            -20.000000        0.062883        0.025100        0.997705   \n",
       "3            -20.000000        0.060717        0.025220        0.997836   \n",
       "4            -20.000000        0.060717        0.025220        0.997836   \n",
       "...                 ...             ...             ...             ...   \n",
       "86180        -20.707271       -0.110854        0.041843        0.992955   \n",
       "86181        -20.707271       -0.110854        0.041843        0.992955   \n",
       "86182        -20.707271       -0.111009        0.041794        0.992940   \n",
       "86183        -20.707271       -0.111009        0.041794        0.992940   \n",
       "86184        -20.707271       -0.111009        0.041794        0.992940   \n",
       "\n",
       "       eye_center1_3d_x  eye_center1_3d_y  eye_center1_3d_z  gaze_normal1_x  \\\n",
       "0            -39.934928         14.997919        -20.075283       -0.150041   \n",
       "1            -39.934928         14.997919        -20.075283       -0.150243   \n",
       "2            -39.934928         14.997919        -20.075283       -0.150210   \n",
       "3            -39.934928         14.997919        -20.075283       -0.149814   \n",
       "4            -39.934928         14.997919        -20.075283       -0.149645   \n",
       "...                 ...               ...               ...             ...   \n",
       "86180        -37.372594         14.763454        -23.294339       -0.130868   \n",
       "86181        -37.372594         14.763454        -23.294339       -0.132037   \n",
       "86182        -37.372594         14.763454        -23.294339       -0.132037   \n",
       "86183        -37.372594         14.763454        -23.294339       -0.133311   \n",
       "86184        -37.372594         14.763454        -23.294339       -0.131320   \n",
       "\n",
       "       gaze_normal1_y  gaze_normal1_z  \n",
       "0            0.234536        0.960458  \n",
       "1            0.234115        0.960530  \n",
       "2            0.233747        0.960624  \n",
       "3            0.233510        0.960744  \n",
       "4            0.233258        0.960831  \n",
       "...               ...             ...  \n",
       "86180        0.093021        0.987026  \n",
       "86181        0.091860        0.986979  \n",
       "86182        0.091860        0.986979  \n",
       "86183        0.091656        0.986827  \n",
       "86184        0.092941        0.986974  \n",
       "\n",
       "[86185 rows x 21 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_copy_1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c84be345",
   "metadata": {},
   "source": [
    "## Exercise4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cf814113",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "427.62610766083765"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算平均采样率为原本的采样率\n",
    "sample_frequence = 1 / ((data[\"gaze_timestamp\"].iloc[-1] - data[\"gaze_timestamp\"].iloc[0]) / len(data))\n",
    "sample_frequence"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc1f990e",
   "metadata": {},
   "source": [
    "## Exercise5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "e2993717",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 按照100Hz的频率重新采样 采样周期为0.01s\n",
    "expect_date = []\n",
    "# 先从第一个点开始采样\n",
    "expect_date.append(data[\"gaze_timestamp\"].loc [0])\n",
    "# 期望的点\n",
    "for i in range(0, len(data)):\n",
    "    tmp = data[\"gaze_timestamp\"].iloc[i] + 0.01\n",
    "    if tmp - expect_date[-1] >= 0.01:\n",
    "        expect_date.append(tmp)\n",
    "\n",
    "# 开始重新采样\n",
    "j = 0\n",
    "result = []\n",
    "for item in expect_date:\n",
    "    while j < len(data) and  abs(data[\"gaze_timestamp\"].loc[j+1] - item) < abs(data[\"gaze_timestamp\"].loc[j] - item):\n",
    "        j += 1\n",
    "    tmp = datetime.datetime.fromtimestamp(data[\"gaze_timestamp\"].loc [j], datetime.timezone.utc).strftime(\"%Y-%m-%dT %H:%M:%S.%f%z\")\n",
    "    result.append(tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "81c85b28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1970-01-02T 00:29:10.776780+0000',\n",
       " '1970-01-02T 00:29:10.787485+0000',\n",
       " '1970-01-02T 00:29:10.798406+0000',\n",
       " '1970-01-02T 00:29:10.807269+0000',\n",
       " '1970-01-02T 00:29:10.818051+0000',\n",
       " '1970-01-02T 00:29:10.831638+0000',\n",
       " '1970-01-02T 00:29:10.843449+0000',\n",
       " '1970-01-02T 00:29:10.858052+0000',\n",
       " '1970-01-02T 00:29:10.869620+0000',\n",
       " '1970-01-02T 00:29:10.879584+0000',\n",
       " '1970-01-02T 00:29:10.891700+0000',\n",
       " '1970-01-02T 00:29:10.905495+0000',\n",
       " '1970-01-02T 00:29:10.914199+0000',\n",
       " '1970-01-02T 00:29:10.927525+0000',\n",
       " '1970-01-02T 00:29:10.938559+0000',\n",
       " '1970-01-02T 00:29:10.947523+0000',\n",
       " '1970-01-02T 00:29:10.961489+0000',\n",
       " '1970-01-02T 00:29:10.973578+0000',\n",
       " '1970-01-02T 00:29:10.982021+0000',\n",
       " '1970-01-02T 00:29:10.995475+0000']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[:20]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
